package com.haiyi.software.app.basic;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.app.R;
import com.haiyi.software.entity.BeanLoginArgs;
import com.haiyi.software.entity.BeanLoginResult;
import com.haiyi.software.entity.BeanRegResult;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.L;

public abstract class LoginActivityBasic extends ActivityBasic {

	private final static String TAG = "LoginActivityBasic";

	private String str_requestURL = null;

	private final String str_requestName = "chklogin.php";

	private BeanLoginArgs loginBody;

	protected JsonElement jsonEle_LoginReturn;

	private Boolean isWork = false;

	public Boolean getIsWork() {

		return isWork;
	}

	@Override
	public synchronized void requestNetworkData(Object... args) {

		if (args.length == 1) {
			loginBody = (BeanLoginArgs) args[0];
		}
		// Log.i("try", getJsonFromObject());
		// TODO 自动生成的方法存根
		if (!isWork) {
			isWork = true;
			str_requestURL = this.getResources().getString(R.string.URL);
			str_requestURL = str_requestURL + str_requestName;
			new ThreadNetWorkTask().execute(null, null, null);
		}
	}

	private class ThreadNetWorkTask extends
			AsyncTask<String, String, List<Map<String, Object>>> {

		@Override
		protected List<Map<String, Object>> doInBackground(String... params) {

			// httpPost连接对象
			HttpPost httpPost = new HttpPost(str_requestURL);
			// 使用NameValuePair来保存要传递的post阐述
			List<NameValuePair> params1 = new ArrayList<NameValuePair>();
			// 添加要传递的参数
			params1.add(new BasicNameValuePair("body", getJsonFromObject()));
			try {
				// 设置字符集
				HttpEntity httpEntity = new UrlEncodedFormEntity(params1,
						"utf-8");
				httpPost.setEntity(httpEntity);
				// 取得默认的HttpClient
				HttpClient httpClient = new DefaultHttpClient();
				// 取得HttpResponse
				HttpResponse httpResponse = httpClient.execute(httpPost);
				// HttpStatus.SC_OK)表示连接成功
				if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
					// 取得返回的字符串
					String result = EntityUtils.toString(
							httpResponse.getEntity(), "UTF-8");

					L.i("login=" + result);

					JsonParser parser = new JsonParser();

					jsonEle_LoginReturn = parser.parse(result.trim());

				} else {

				}
			} catch (ClientProtocolException e) {
				Log.e("PostActivity", "ClientProtocolException");
				e.printStackTrace();
			} catch (IOException e) {
				Log.e("PostActivity", "IOException");
				e.printStackTrace();
			}
			return null;
		}

		@Override
		protected void onPostExecute(List<Map<String, Object>> result) {

			super.onPostExecute(result);
			requestFinish(null);
			isWork = false;
		}

	}

	private String getJsonFromObject() {

		Gson gson = new Gson();

		String lsString = gson.toJson(loginBody, BeanLoginArgs.class);

		Log.i(TAG, "args" + lsString);
		return lsString;
	}

}
